home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / FGFDEMO.ZIP / FGFDEMO.C < prev    next >
Text File  |  1993-07-17  |  19KB  |  630 lines

  1. /****************************************************************************\
  2. *                                                                            *
  3. *  FGFDEMO.C                                                                 *
  4. *                                                                            *
  5. *  This program demonstrates some features of Fastgraph/Fonts.               *
  6. *                                                                            *
  7. *  Fastgraph/Fonts lets you easily add bit-mapped font support to Fastgraph  *
  8. *  or Fastgraph/Light applications.                                          *
  9. *                                                                            *
  10. *  Copyright (c) 1992-1993 Ted Gruber Software.  All Rights Reserved.        *
  11. *                                                                            *
  12. *                                                                            *
  13. *  Ted Gruber Software would like to acknowledge the contributions made by   *
  14. *  Randall Dryburgh of Micron Software Sciences in creating FGFDEMO.  Randy  *
  15. *  developed the original versions of the functions relating to the palette  *
  16. *  fades and the digital odometer.                                           *
  17. *                                                                            *
  18. \****************************************************************************/
  19.  
  20. #define NFONTS 11
  21. #define NPALETTES 16
  22. #define NSTEPS 32
  23.  
  24. #define FALSE 0
  25. #define TRUE  1
  26.  
  27. #define LEFT   -1
  28. #define CENTER  0
  29. #define RIGHT   1
  30. #define TOP     1
  31. #define BOTTOM -1
  32.  
  33. #include <fastgraf.h>
  34. #include <fgfonts.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <time.h>
  39.  
  40. /****************************************************************************\
  41. *                                                                            *
  42. *  function prototypes                                                       *
  43. *                                                                            *
  44. \****************************************************************************/
  45.  
  46. void main(void);
  47.  
  48. void average_palette(void);
  49. void fade_in(int,int);
  50. void fade_out(int,int);
  51. void odometer(int,int,double,int,int);
  52. int  widest_numeral(void);
  53.  
  54. /****************************************************************************\
  55. *                                                                            *
  56. *  font handles, font names, and other global variables                      *
  57. *                                                                            *
  58. \****************************************************************************/
  59.  
  60. int austin, broadway, cognac, crystal14, crystal26, fountain, modern, plaza;
  61. int regal, royal, standard;
  62.  
  63. char *fontname[NFONTS] = {
  64.    "Austin 36",
  65.    "Broadway 18",
  66.    "Cognac 19",
  67.    "Crystal 14",
  68.    "Crystal 26",
  69.    "Fountain 27",
  70.    "Modern 28",
  71.    "Plaza 14",
  72.    "Regal 24",
  73.    "Royal 15",
  74.    "Standard 8"};
  75.  
  76. unsigned int clockspeed;
  77.  
  78. char default_palette[NPALETTES*3];
  79. char new_palette[NPALETTES*3];
  80. char zeroes[NPALETTES*3];
  81.  
  82. float average[NPALETTES*3];
  83.  
  84. /****************************************************************************\
  85. *                                                                            *
  86. *  main program                                                              *
  87. *                                                                            *
  88. \****************************************************************************/
  89.  
  90. void main()
  91. {
  92.    register int i;
  93.    int abort;
  94.    int old_mode;
  95.    int width;
  96.    int x, y;
  97.    double amount;
  98.    unsigned char key, aux;
  99.    char string[24];
  100.  
  101.    /* make sure we're running on a VGA system; exit if not */
  102.  
  103.    if (fg_testmode(18,0) == 0)
  104.    {
  105.       printf("This demo requires 640 x 480 16 color VGA graphics.\n");
  106.       exit(1);
  107.    }
  108.  
  109.    /* load the font files */
  110.  
  111.    austin    = fgf_load("AUSTIN36.FGF");
  112.    broadway  = fgf_load("BRODWY18.FGF");
  113.    cognac    = fgf_load("COGNAC19.FGF");
  114.    crystal14 = fgf_load("CRYSTL14.FGF");
  115.    crystal26 = fgf_load("CRYSTL26.FGF");
  116.    fountain  = fgf_load("FOUNTN27.FGF");
  117.    modern    = fgf_load("MODERN28.FGF");
  118.    plaza     = fgf_load("PLAZA14.FGF");
  119.    regal     = fgf_load("REGAL24.FGF");
  120.    royal     = fgf_load("ROYAL15.FGF");
  121.    standard  = fgf_load("STNDRD08.FGF");
  122.  
  123.    /* verify all fonts were loaded successfully; exit if not */
  124.  
  125.    abort = FALSE;
  126.    if (austin == 0) abort = TRUE;
  127.    if (broadway == 0) abort = TRUE;
  128.    if (cognac == 0) abort = TRUE;
  129.    if (crystal14 == 0) abort = TRUE;
  130.    if (crystal26 == 0) abort = TRUE;
  131.    if (fountain == 0) abort = TRUE;
  132.    if (modern == 0) abort = TRUE;
  133.    if (plaza == 0) abort = TRUE;
  134.    if (regal == 0) abort = TRUE;
  135.    if (royal == 0) abort = TRUE;
  136.    if (standard == 0) abort = TRUE;
  137.    if (abort)
  138.    {
  139.       printf("Failure loading one or more font files.\n");
  140.       exit(1);
  141.    }
  142.  
  143.    /* benchmark the system speed */
  144.  
  145.    clockspeed = fg_measure();
  146.  
  147.    /* initialize the array that zeroes the DAC values */
  148.  
  149.    for (i = 0; i < NPALETTES*3; i++)
  150.       zeroes[i] = 0;
  151.  
  152.    /* initialize Fastgraph's video environment */
  153.  
  154.    old_mode = fg_getmode();
  155.    fg_setmode(18);
  156.    fg_getdacs(0,NPALETTES,default_palette);
  157.  
  158.    /* create palette increment averages */
  159.  
  160.    average_palette();
  161.  
  162.    /* draw the TGS logo on the hidden page */
  163.  
  164.    fg_setpage(1);
  165.    fg_showpcx("TGS.PCX",0);
  166.  
  167.    /* fade in the TGS logo on the visual page */
  168.  
  169.    fg_setdacs(0,NPALETTES,zeroes);
  170.    fg_transfer(0,185,0,89,227,285,1,0);
  171.    fade_in(0,16);
  172.  
  173.    /* fade in "and" */
  174.  
  175.    fg_setpage(0);
  176.    fg_setrgb(15,0,0,0);
  177.    fgf_select(standard);
  178.    fg_setcolor(15);
  179.    fg_move(320,315);
  180.    fgf_justify(CENTER,CENTER);
  181.    fgf_print("and",3);
  182.    fade_in(15,1);
  183.  
  184.    /* fade out the visual page */
  185.  
  186.    fg_waitfor(30);
  187.    fade_out(0,16);
  188.  
  189.    /* erase both pages */
  190.  
  191.    fg_setpage(0);
  192.    fg_erase();
  193.    fg_setpage(1);
  194.    fg_erase();
  195.  
  196.    /* draw the Micron logo on the hidden page */
  197.  
  198.    fg_move(0,0);
  199.    fg_showpcx("MICRON.PCX",2);
  200.  
  201.    /* fade in the Micron logo on the visual page */
  202.  
  203.    fg_setdacs(0,NPALETTES,zeroes);
  204.    fg_transfer(0,165,0,52,237,266,1,0);
  205.    fade_in(0,16);
  206.  
  207.    /* fade in "present" */
  208.  
  209.    fg_setpage(0);
  210.    fg_setrgb(14,0,0,0);
  211.    fg_setcolor(14);
  212.    fg_move(320,315);
  213.    fgf_print("present",7);
  214.    fade_in(14,1);
  215.  
  216.    /* fade to black */
  217.  
  218.    fg_waitfor(30);
  219.    fade_out(0,16);
  220.  
  221.    /* erase both pages */
  222.  
  223.    fg_setpage(0);
  224.    fg_erase();
  225.    fg_setpage(1);
  226.    fg_erase();
  227.  
  228.    /* display and then fade out the Fastgraph/Fonts logo */
  229.  
  230.    fg_setpage(0);
  231.    fgf_select(austin);
  232.    fg_setcolor(10);
  233.    fg_move(320,240);
  234.    fgf_justify(CENTER,BOTTOM);
  235.    fgf_print("Fastgraph/Fonts",15);
  236.    fgf_select(crystal14);
  237.    fg_setcolor(15);
  238.    fg_move(320,270);
  239.    fgf_justify(CENTER,CENTER);
  240.    fgf_print("Copyright (c) 1992-1993 Ted Gruber Software",43);
  241.    fg_move(320,286);
  242.    fgf_print("All Rights Reserved.",20);
  243.    fade_in(10,1);
  244.    fg_waitfor(18);
  245.    fade_in(15,1);
  246.    fg_waitfor(30);
  247.    fade_out(0,16);
  248.  
  249.    /* display the info screen */
  250.  
  251.    fg_setpage(0);
  252.    fg_erase();
  253.  
  254.    fgf_select(regal);
  255.    fg_setcolor(10);
  256.    fg_box(0,639,0,479);
  257.    fg_move(0,32);
  258.    fg_draw(639,32);
  259.    fg_move(320,5);
  260.    fgf_justify(CENTER,TOP);
  261.    fgf_print("Fastgraph/Fonts",15);
  262.  
  263.    fgf_select(modern);
  264.    fg_setcolor(12);
  265.    fg_move(320,60);
  266.    fgf_justify(CENTER,CENTER);
  267.    fgf_print("Fastgraph/Fonts\177\011 lets you easily add bit-mapped",48);
  268.    fg_move(320,90);
  269.    fgf_print("character support to Fastgraph applications.  It",48);
  270.    fg_move(320,120);
  271.    fgf_print("includes a wide range of fonts in several point sizes.",54);
  272.    fg_move(320,150);
  273.    fgf_print("An application can load up to 32 fonts at once.",47);
  274.  
  275.    fgf_select(broadway);
  276.    fg_setcolor(12);
  277.    fg_move(320,190);
  278.    fgf_print("Fastgraph/Fonts\177\017 includes functions for font loading",53);
  279.    fg_move(320,215);
  280.    fgf_print("and unloading, string display with horizontal and",49);
  281.    fg_move(320,240);
  282.    fgf_print("vertical justification, font selection, determining",51);
  283.    fg_move(320,265);
  284.    fgf_print("string height and width, and other useful features.",51);
  285.    fg_move(320,290);
  286.    fgf_print("Font files that come with Fastgraph/Fonts may be",48);
  287.    fg_move(320,315);
  288.    fgf_print("distributed freely as part of your applications.",48);
  289.  
  290.    fgf_select(fountain);
  291.    fg_setcolor(9);
  292.    fg_move(320,355);
  293.    fgf_print("The \177\014Fastgraph/Fonts User's Guide\177\011 includes a",47);
  294.    fg_move(320,385);
  295.    fgf_print("description of the font file format, so you can",47);
  296.    fg_move(320,415);
  297.    fgf_print("create your own font files in case Fastgraph/Fonts",50);
  298.    fg_move(320,445);
  299.    fgf_print("doesn't include your favorite fonts.",36);
  300.  
  301.    fade_in(0,16);
  302.    fg_waitkey();
  303.    fade_out(0,16);
  304.    fg_erase();
  305.  
  306.    /* display the features screen */
  307.  
  308.    fgf_select(cognac);
  309.    fg_setcolor(14);
  310.    fg_move(320,240);
  311.    fgf_print("...and now to demonstrate some Fastgraph/Fonts features...",58);
  312.    fade_in(14,1);
  313.    fg_waitfor(30);
  314.    fade_out(14,1);
  315.    fg_setcolor(1);
  316.    fg_setpage(1);
  317.    fg_rect(0,639,0,319);
  318.    fg_setpage(0);
  319.    fg_rect(0,639,0,479);
  320.    fg_setdacs(0,NPALETTES,default_palette);
  321.  
  322.    fgf_select(crystal26);
  323.    fg_setcolor(15);
  324.    fg_move(320,50);
  325.    fgf_justify(CENTER,BOTTOM);
  326.    fgf_print("Load up to 32 fonts at once!",28);
  327.  
  328.    fgf_select(crystal26);
  329.    fg_setcolor(4);
  330.    fg_move(240,90);
  331.    fgf_print("Jackpot is $ ",13);
  332.    x = fg_getxpos();
  333.    y = fg_getypos();
  334.  
  335.    fgf_select(broadway);
  336.    fg_setcolor(2);
  337.    fg_move(320,120);
  338.    fgf_print("Change \177\003colors \177\004anywhere \177\005in \177\006a \177\007string",44);
  339.  
  340.    fgf_select(royal);
  341.    fg_setcolor(14);
  342.    fg_move(320,160);
  343.    fgf_print("Justify strings horizontally and vertically:",44);
  344.    fg_setcolor(7);
  345.    fg_move(0,180);
  346.    fg_dash(639,180,0x1111);
  347.    fg_setcolor(14);
  348.    fg_move(0,180);
  349.    fgf_justify(LEFT,BOTTOM);
  350.    fgf_print("LEFT AND ABOVE",14);
  351.    fg_move(320,180);
  352.    fgf_justify(CENTER,CENTER);
  353.    fgf_print("CENTERED BOTH DIRECTIONS",24);
  354.    fg_move(639,180);
  355.    fgf_justify(RIGHT,TOP);
  356.    fgf_print("RIGHT AND BELOW",15);
  357.  
  358.    fgf_select(modern);
  359.    fg_setcolor(12);
  360.    fg_move(320,220);
  361.    width = fgf_width(" ",1);
  362.    fgf_space(width/2);
  363.    fgf_justify(CENTER,CENTER);
  364.    fgf_print("narrow spacing between words",28);
  365.    fg_move(320,250);
  366.    fgf_space(width);
  367.    fgf_print("normal spacing between words",28);
  368.    fg_move(320,280);
  369.    fgf_space(width*2);
  370.    fgf_print("wide spacing between words",26);
  371.  
  372.    do
  373.       fg_intkey(&key,&aux);
  374.    while (key+aux > 0);
  375.  
  376.    fgf_select(crystal26);
  377.    fg_setcolor(4);
  378.    amount = 1998.31;
  379.    do
  380.    {
  381.       odometer(x,y,amount,4,1);
  382.       amount += 0.01;
  383.       fg_intkey(&key,&aux);
  384.    } while (key+aux == 0 && amount < 10000.00);
  385.  
  386.    /* cast of characters screen */
  387.  
  388.    fg_setpage(0);
  389.    fg_erase();
  390.    fgf_select(regal);
  391.    fg_setcolor(10);
  392.    fg_move(320,0);
  393.    fgf_justify(CENTER,TOP);
  394.    fgf_print("*** Partial Cast of Characters ***",34);
  395.  
  396.    strcpy(string,"ABCDabcd1234.,?!+-&@#$");
  397.    y = 80;
  398.  
  399.    for (i = 0; i < NFONTS; i++)
  400.    {
  401.       fgf_select(i+1);
  402.       fg_setcolor(i+1);
  403.       fg_move(20,y);
  404.       fgf_justify(LEFT,BOTTOM);
  405.       fgf_print(fontname[i],strlen(fontname[i]));
  406.       fg_move(620,y);
  407.       fgf_justify(RIGHT,BOTTOM);
  408.       fgf_print(string,22);
  409.       y += 36;
  410.    }
  411.  
  412.    fg_setcolor(10);
  413.    fg_move(320,y);
  414.    fgf_justify(CENTER,BOTTOM);
  415.    fgf_print("and many more!",14);
  416.  
  417.    fg_waitkey();
  418.    fade_out(0,16);
  419.  
  420.    /* unload fonts and restore the original video state before exiting */
  421.  
  422.    fgf_unload(-1);
  423.    fg_setmode(old_mode);
  424.    fg_reset();
  425.  
  426.    /* display ordering information */
  427.  
  428.    printf("Fastgraph/Fonts (tm) is available for $49 from:\n\n");
  429.    printf("Ted Gruber Software     orders/info (702) 735-1980\n");
  430.    printf("PO Box 13408                    FAX (702) 735-4603\n");
  431.    printf("Las Vegas, NV  89112            BBS (702) 796-7134\n\n");
  432.    printf("Please add $3 shipping within the U.S. and Canada,\n");
  433.    printf("or $6 to other countries.\n");
  434. }
  435.  
  436. /****************************************************************************\
  437. *                                                                            *
  438. *  average_palette                                                           *
  439. *                                                                            *
  440. *  Compute the palette fade increments used by fade_in and fade_out.         *
  441. *                                                                            *
  442. \****************************************************************************/
  443.  
  444. void average_palette()
  445. {
  446.    register int i;
  447.  
  448.    for (i = 0; i < NPALETTES*3 ; i++)
  449.       average[i] = (float)(default_palette[i]) / (float)(NSTEPS);
  450. }
  451.  
  452. /****************************************************************************\
  453. *                                                                            *
  454. *  fade_in                                                                   *
  455. *                                                                            *
  456. *  Fade one or more DACs from black to their target colors.                  *
  457. *                                                                            *
  458. \****************************************************************************/
  459.  
  460. void fade_in(start,count)
  461. int start, count;
  462. {
  463.    register int i, j;
  464.    int k, n;
  465.    int last;
  466.  
  467.    last = start + count;
  468.  
  469.    for (i = 0; i < NSTEPS; i++)
  470.    {
  471.       k = 0;
  472.       n = start * 3;
  473.       for (j = start; j < last; j++)
  474.       {
  475.          new_palette[k++] = (char)(average[n++] * i);
  476.          new_palette[k++] = (char)(average[n++] * i);
  477.          new_palette[k++] = (char)(average[n++] * i);
  478.       }
  479.       fg_setdacs(start,count,new_palette);
  480.       fg_waitfor(1);
  481.    }
  482. }
  483.  
  484. /****************************************************************************\
  485. *                                                                            *
  486. *  fade_out                                                                  *
  487. *                                                                            *
  488. *  Fade one or more DACs from their current colors to black.                 *
  489. *                                                                            *
  490. \****************************************************************************/
  491.  
  492. void fade_out(start,count)
  493. int start, count;
  494. {
  495.    register int i, j;
  496.    int k, n;
  497.    int last;
  498.  
  499.    last = start + count;
  500.  
  501.    for (i = 0; i < NSTEPS; i++)
  502.    {
  503.       k = 0;
  504.       n = start * 3;
  505.       for (j = start; j < last; j++)
  506.       {
  507.          new_palette[k++] = default_palette[n] - (char)(average[n] * i);
  508.          n++;
  509.          new_palette[k++] = default_palette[n] - (char)(average[n] * i);
  510.          n++;
  511.          new_palette[k++] = default_palette[n] - (char)(average[n] * i);
  512.          n++;
  513.       }
  514.       fg_setdacs(start,count,new_palette);
  515.       fg_waitfor(1);
  516.    }
  517. }
  518.  
  519. /****************************************************************************\
  520. *                                                                            *
  521. *  odometer                                                                  *
  522. *                                                                            *
  523. *  Increment the odometer amount by one cent and scroll its new amount on    *
  524. *  the screen.                                                               *
  525. *                                                                            *
  526. \****************************************************************************/
  527.  
  528. void odometer(x,y,amount,foreground_color,background_color)
  529. int x, y;
  530. double amount;
  531. int foreground_color, background_color;
  532. {
  533.    register int i, j;
  534.    int char_width;
  535.    int delay_diff, delay_same;
  536.    int height, width;
  537.    int length_old, length_new;
  538.    int xpos;
  539.    char amount_old[20], amount_new[20];
  540.  
  541.    /* create strings for old and new amounts */
  542.  
  543.    sprintf(amount_old,"%.2f",amount);
  544.    sprintf(amount_new,"%.2f",amount+0.01);
  545.    length_old = strlen(amount_old);
  546.    length_new = strlen(amount_new);
  547.  
  548.    /* get height of the amount string */
  549.  
  550.    height = fgf_height(amount_old,length_old);
  551.  
  552.    /* get its width, rounded up to a byte boundary multiple */
  553.  
  554.    width = (widest_numeral() + 7) & 0xFFF8;
  555.  
  556.    /* create a box in the background color */
  557.  
  558.    fg_setpage(1);
  559.    fg_setcolor(background_color);
  560.    fg_rect(200,200+width*length_new,100-height,100+height);
  561.    fg_setcolor(foreground_color);
  562.    fgf_justify(LEFT,BOTTOM);
  563.  
  564.    /* put old amount on hidden video page */
  565.  
  566.    for (i = 0; i < length_old; i++)
  567.    {
  568.       char_width = (width - fgf_width(&amount_old[i],1)) / 2;
  569.       fg_move(200+i*width+char_width,100);
  570.       fgf_print(&amount_old[i],1);
  571.    }
  572.  
  573.    /* put new amount on hidden video page */
  574.  
  575.    for (i = 0; i < length_new; i++)
  576.    {
  577.       char_width = (width - fgf_width(&amount_new[i],1)) / 2;
  578.       fg_move(200+i*width+char_width,100+height);
  579.       fgf_print(&amount_new[i],1);
  580.    }
  581.  
  582.    /* copy old amount to visual video page */
  583.  
  584.    fg_transfer(200,200+width*length_old,100-height,100,x,y,1,0);
  585.  
  586.    /* roll the odometer */
  587.  
  588.    delay_diff = (clockspeed / 60) / length_new;
  589.    delay_same = (clockspeed / 10) / length_new;
  590.  
  591.    for (j = 0; j < height; j++)
  592.    {
  593.       for (i = 0; i < length_new; i++)
  594.       {
  595.          if (amount_new[i] != amount_old[i] && amount_old[i] != '.')
  596.          {
  597.             fg_stall(delay_diff);
  598.             xpos = 200 + i * width;
  599.             fg_transfer(xpos,xpos+width,100+j-height,100+j,x+xpos-200,y,1,0);
  600.          }
  601.          else
  602.             fg_stall(delay_same);
  603.       }
  604.    }
  605. }
  606.  
  607. /****************************************************************************\
  608. *                                                                            *
  609. *  widest_numeral                                                            *
  610. *                                                                            *
  611. *  Compute the width of the widest digit in the current font.                *
  612. *                                                                            *
  613. \****************************************************************************/
  614.  
  615. int widest_numeral()
  616. {
  617.    int widest;
  618.    int width;
  619.    char c;
  620.  
  621.    widest = 0;
  622.    for (c = '0'; c <= '9'; c++)
  623.    {
  624.       width = fgf_width(&c,1);
  625.       if (width > widest) widest = width;
  626.    }
  627.  
  628.    return(width);
  629. }
  630.